package com.example.springboot.utils;

import org.apache.commons.lang.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * imsi获取手机号归属地
 * 
 * @author wangsh
 * 
 */
public class IMSIUtil {
	public static String getMobileAll(String imsi) {
		String s130 = "^46001(\\d{3})(\\d)[0,1]\\d+";
		String s131 = "^46001(\\d{3})(\\d)9\\d+";
		String s132 = "^46001(\\d{3})(\\d)2\\d+";
		String s134 = "^460020(\\d)(\\d{3})\\d+";
		String s13x0 = "^46000(\\d{3})([5,6,7,8,9])\\d+";
		String s13x = "^46000(\\d{3})([0,1,2,3,4])(\\d)\\d+";
		String s150 = "^460023(\\d)(\\d{3})\\d+";
		String s151 = "^460021(\\d)(\\d{3})\\d+";
		String s152 = "^460022(\\d)(\\d{3})\\d+";
		String s155 = "^46001(\\d{3})(\\d)4\\d+";
		String s156 = "^46001(\\d{3})(\\d)3\\d+";
		String s157 = "^460077(\\d)(\\d{3})\\d+";
		String s158 = "^460028(\\d)(\\d{3})\\d+";
		String s159 = "^460029(\\d)(\\d{3})\\d+";
		String s147 = "^460079(\\d)(\\d{3})\\d+";
		String s185 = "^46001(\\d{3})(\\d)5\\d+";
		String s186 = "^46001(\\d{3})(\\d)6\\d+";
		String s187 = "^460027(\\d)(\\d{3})\\d+";
		String s188 = "^460078(\\d)(\\d{3})\\d+";
		String s1705 = "^460070(\\d)(\\d{3})\\d+";
		String s170x = "^46001(\\d{3})(\\d)8\\d+";
		String s178 = "^460075(\\d)(\\d{3})\\d+";
		String s145 = "^46001(\\d{3})(\\d)7\\d+";
		String s182 = "^460026(\\d)(\\d{3})\\d+";
		String s183 = "^460025(\\d)(\\d{3})\\d+";
		String s184 = "^460024(\\d)(\\d{3})\\d+";

		String s180 = "^46003(\\d)(\\d{3})7\\d+";
		String s153 = "^46003(\\d)(\\d{3})8\\d+";
		String s189 = "^46003(\\d)(\\d{3})9\\d+";

		String[] result = compile(s130, imsi);
		if ((result != null) && (result.length == 2)) {
			return "130" + result[1] + result[0];
		}
		result = compile(s131, imsi);
		if ((result != null) && (result.length == 2)) {
			return "131" + result[1] + result[0];
		}
		result = compile(s132, imsi);
		if ((result != null) && (result.length == 2)) {
			return "132" + result[1] + result[0];
		}
		result = compile(s134, imsi);
		if ((result != null) && (result.length == 2)) {
			return "134" + result[0] + result[1];
		}
		result = compile(s13x0, imsi);
		if ((result != null) && (result.length == 2)) {
			return "13" + result[1] + "0" + result[0];
		}
		result = compile(s13x, imsi);
		if ((result != null) && (result.length == 3)) {
			return "13" + (Integer.parseInt(result[1]) + 5) + result[2] + result[0];
		}
		result = compile(s150, imsi);
		if ((result != null) && (result.length == 2)) {
			return "150" + result[0] + result[1];
		}
		result = compile(s151, imsi);
		if ((result != null) && (result.length == 2)) {
			return "151" + result[0] + result[1];
		}
		result = compile(s152, imsi);
		if ((result != null) && (result.length == 2)) {
			return "152" + result[0] + result[1];
		}
		result = compile(s155, imsi);
		if ((result != null) && (result.length == 2)) {
			return "155" + result[1] + result[0];
		}
		result = compile(s156, imsi);
		if ((result != null) && (result.length == 2)) {
			return "156" + result[1] + result[0];
		}
		result = compile(s157, imsi);
		if ((result != null) && (result.length == 2)) {
			return "157" + result[0] + result[1];
		}
		result = compile(s158, imsi);
		if ((result != null) && (result.length == 2)) {
			return "158" + result[0] + result[1];
		}
		result = compile(s159, imsi);
		if ((result != null) && (result.length == 2)) {
			return "159" + result[0] + result[1];
		}
		result = compile(s147, imsi);
		if ((result != null) && (result.length == 2)) {
			return "147" + result[0] + result[1];
		}
		result = compile(s185, imsi);
		if ((result != null) && (result.length == 2)) {
			return "185" + result[1] + result[0];
		}
		result = compile(s186, imsi);
		if ((result != null) && (result.length == 2)) {
			return "186" + result[1] + result[0];
		}
		result = compile(s187, imsi);
		if ((result != null) && (result.length == 2)) {
			return "187" + result[0] + result[1];
		}
		result = compile(s188, imsi);
		if ((result != null) && (result.length == 2)) {
			return "188" + result[0] + result[1];
		}
		result = compile(s1705, imsi);
		if ((result != null) && (result.length == 2)) {
			return "170" + result[0] + result[1];
		}
		result = compile(s170x, imsi);
		if ((result != null) && (result.length == 2)) {
			return "170" + result[1] + result[0];
		}
		result = compile(s178, imsi);
		if ((result != null) && (result.length == 2)) {
			return "178" + result[0] + result[1];
		}
		result = compile(s145, imsi);
		if ((result != null) && (result.length == 2)) {
			return "145" + result[1] + result[0];
		}
		result = compile(s182, imsi);
		if ((result != null) && (result.length == 2)) {
			return "182" + result[0] + result[1];
		}
		result = compile(s183, imsi);
		if ((result != null) && (result.length == 2)) {
			return "183" + result[0] + result[1];
		}
		result = compile(s184, imsi);
		if ((result != null) && (result.length == 2)) {
			return "184" + result[0] + result[1];
		}
		result = compile(s180, imsi);
		if ((result != null) && (result.length == 2)) {
			return "180" + result[0] + result[1];
		}
		result = compile(s153, imsi);
		if ((result != null) && (result.length == 2)) {
			return "153" + result[0] + result[1];
		}
		// result = compile(s189, imsi);
		// if ((result != null) && (result.length == 2)) {
		// return "189" + result[0] + result[1];
		// }
		return "";
	}

	private static String[] compile(String reg, String imsi) {
		Pattern pattern = Pattern.compile(reg);
		Matcher matcher = pattern.matcher(imsi);

		if (matcher.find()) {
			String[] sArr = new String[matcher.groupCount()];
			for (int i = 0; i < matcher.groupCount(); i++) {
				sArr[i] = matcher.group(i + 1);
			}
			return sArr;
		}
		return null;
	}

	/**
	 * 获取imsi厂商
	 * 
	 * @Description:
	 * @date:Dec 15, 2017 10:39:21 AM
	 * @param imsi
	 * @return
	 * @return String 返回类型
	 * @throws
	 */
	public static String getMobileOperator(String imsi) {
		if (imsi != null) {
			if (imsi.startsWith("46000") || imsi.startsWith("46002") || imsi.startsWith("46004") || imsi.startsWith("46007")) {
				// 因为移动网络编号46000下的IMSI已经用完，所以虚拟了一个46002编号，134/159号段使用了此编号
				// 中国移动
				return "中国移动";
			} else if (imsi.startsWith("46001") || imsi.startsWith("46006") || imsi.startsWith("46009")) {
				// 中国联通
				return "中国联通";
			} else if (imsi.startsWith("46003") || imsi.startsWith("46005") || imsi.startsWith("46011")) {
				// 中国电信
				return "中国电信";
			} else if (imsi.startsWith("46020")) {
				// 中国电信
				return "中国铁通";
			}
		}
		return "";
	}

	/**
	 * 获取手机号厂商 通过IMSI前五位可以知道运营商；01 中国电信;02 中国网通;03 中国联通 04 中国长城宽带;05 中国铁通;06
	 * 中国移动 07 中国卫星集团;08 教育部门;09 中科院;99 其它
	 * 
	 * @Description:
	 * @date:Dec 15, 2017 10:26:06 AM
	 * @param MobileOperator
	 * @return
	 * @return String 返回类型
	 * @throws
	 */
	public static String getIMSIOperator(String mobileOperator) {
		if (StringUtils.isEmpty(mobileOperator)) {
			return "";
		}
		mobileOperator = mobileOperator.trim();
		if ("1".equals(mobileOperator) || "01".equals(mobileOperator)) {
			return "中国电信";
		} else if ("2".equals(mobileOperator) || "02".equals(mobileOperator)) {
			return "中国网通";
		} else if ("3".equals(mobileOperator) || "03".equals(mobileOperator)) {
			return "中国联通";
		} else if ("4".equals(mobileOperator) || "04".equals(mobileOperator)) {
			return "中国长城宽带";
		} else if ("5".equals(mobileOperator) || "05".equals(mobileOperator)) {
			return "中国铁通";
		} else if ("6".equals(mobileOperator) || "06".equals(mobileOperator)) {
			return "中国移动";
		} else if ("7".equals(mobileOperator) || "07".equals(mobileOperator)) {
			return "中国卫星集团";
		} else if ("8".equals(mobileOperator) || "08".equals(mobileOperator)) {
			return "教育部门";
		} else if ("9".equals(mobileOperator) || "09".equals(mobileOperator)) {
			return "中科院";
		} else if ("99".equals(mobileOperator)) {
			return "其它";
		}
		return "";
	}

	/**
	 * 获取手机号厂商
	 * 
	 * @Description:
	 * @date:Dec 15, 2017 10:26:06 AM
	 * @param MobileOperator
	 * @return
	 * @return String 返回类型
	 * @throws
	 */
	public static String getMobileOperator(String imsi, String mobileOperator) {
		String operator = IMSIUtil.getIMSIOperator(mobileOperator);
		if (StringUtils.isEmpty(operator)) {
			operator = IMSIUtil.getMobileOperator(imsi);
		}
		return operator;
	}

}
